//
//  BindPhoneVC.swift
//  BaseSwift
//
//  Created by gh on 2019/10/8.
//  Copyright © 2019 ghwang. All rights reserved.
//

import UIKit

class BindPhoneVC: UIViewController,UITextFieldDelegate {

    let tf = UITextField()
    let commitBtn = UIButton()
    

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.navigationView.setTitle("")
        self.view.backgroundColor = UIColor.white
        self.navigationView.removeAllLeftButton()
        self.navigationView.addLeftButton(with: UIImage(named: "关闭")) { (view) in
            
            if (self.navigationController?.viewControllers.count)! > 1{
                
                POP(vc: self)

            }else{
                
                APP.initMainVC()
            }
        }
        
        let icon = UIImageView()
        icon.image = UIImage(named: "默认头像")
        icon.sd_cornerRadius = 32
        view.addSubview(icon)
        
        
        if SPUtil.init().headImg != nil{
              
            icon.sd_setImage(with: URL(string:SPUtil.init().headImg), placeholderImage: UIImage(named: "默认头像"), options: .lowPriority) { (A, B, C, D) in
                  
              }
          }
        
        
        let _ = icon.sd_layout()?.widthIs(64)!.heightIs(64)!.centerXEqualToView(view)!.topSpaceToView(view,HT_StatusBarAndNavigationBarHeight+35)
        
        let nameLab = UILabel()
        nameLab.font = UIFont.titleFont
        nameLab.textColor = UIColor.textTitleColorBlack
        nameLab.text = SPUtil.init().nickName
        nameLab.textAlignment = .center
        view.addSubview(nameLab)
        let _ = nameLab.sd_layout()?.topSpaceToView(icon,16)!.widthIs(155)!.heightIs(22)!.centerXEqualToView(view)
        
        let introLab = UILabel()
        introLab.text = "为了账号安全,请绑定手机号"
        introLab.textColor = UIColor.textTitleColor
        introLab.font = UIFont.commonFontSmaller
        view.addSubview(introLab)
        let _ = introLab.sd_layout()?.leftSpaceToView(view,32)!.heightIs(20)!.topSpaceToView(nameLab,35)!.rightSpaceToView(view,32)
        
        
        let inputBg = UIView()
        self.view.addSubview(inputBg)
        let _ = inputBg.sd_layout()?.leftSpaceToView(view,32)!.rightSpaceToView(view,32)!.topSpaceToView(introLab,25)!.heightIs(45)
        
        let cBtn = UIButton()
        cBtn.setTitle("+86", for: .normal)
        inputBg.addSubview(cBtn)
        cBtn.setTitleColor(UIColor.textTitleColor, for: .normal)
        cBtn.titleLabel?.font = UIFont.commonFontSmaller
        let _ = cBtn.sd_layout()?.leftSpaceToView(inputBg,0)!.heightIs(30)!.centerYEqualToView(inputBg)!.widthIs(35)
        
        tf.font = UIFont.systemFont(ofSize: 20)
        tf.clearButtonMode = .whileEditing
        tf.delegate = self
        tf.textColor = UIColor.textTitleColorBlack
        tf.placeholder = "请输入手机号"
        inputBg.addSubview(tf)
        let _ = tf.sd_layout()?.leftSpaceToView(cBtn,16)!.rightSpaceToView(inputBg,16)!.heightIs(35)!.centerYEqualToView(inputBg)
        tf.addTarget(self, action: #selector(textFieldEditingChanged), for: .editingChanged)

        
        
        let bline = UIView()
        bline.backgroundColor = UIColor.lineColor
        inputBg.addSubview(bline)
        let _ = bline.sd_layout()?.widthRatioToView(inputBg,1)!.heightIs(1)!.bottomSpaceToView(inputBg,0)
        
        
        
        commitBtn.addTarget(self, action: #selector(getCodeClick), for: .touchUpInside)
        view.addSubview(commitBtn)
        commitBtn.isEnabled = false
        commitBtn.sd_cornerRadius = LARAGE_BTN_RADIUS
        let _ = commitBtn.sd_layout()?.leftSpaceToView(view,24)?.rightSpaceToView(view,24)?.heightIs(LARAGE_BTN_HEIGHT)?.centerXEqualToView(view)?.topSpaceToView(inputBg,35)
        commitBtn.backgroundColor = UIColor.init(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.2)

        let tlab = UILabel()
        tlab.text = "获取验证码"
        tlab.tag = 9090
        tlab.font = UIFont.commonFont
        tlab.textColor = UIColor.white
        tlab.textAlignment = .center
        commitBtn.addSubview(tlab)
        let _ = tlab.sd_layout()?.spaceToSuperView(UIEdgeInsets.zero)
        
        
    }
    @objc func textFieldEditingChanged(textfield:UITextField){
        
        if tf.text?.phoneValidate == true {
            
            commitBtn.gradientBtnMainColor()
            commitBtn.isEnabled = true
            let lab = commitBtn.viewWithTag(9090)
            commitBtn.bringSubviewToFront(lab!)

        }else{
            
            commitBtn.backgroundColor = UIColor.init(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.2)
            commitBtn.isEnabled = false
            for item in commitBtn.layer.sublayers!{
                
                if item is CAGradientLayer{
                    
                    item.removeFromSuperlayer()
                }
            }

        }
    }
    @objc func getCodeClick(){
        
        if tf.text?.phoneValidate == false{
            
            ToastUtil.show(msg: "请输入有效的手机号")
            return
        }
        
        API.sendCode(params: ["mobile":tf.text!,
                                  ]) { (any) in
                                    
                                    ToastUtil.show(msg: "发送成功，请查收短信")
                                    let dvc = InputCodeVC()
                                    dvc.type = "bindwx"
                                    dvc.phone = self.tf.text!
                                    PUSH(from: self, to: dvc)

        }
        
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
